<%@ page import="java.util.Map" %>
<%@ page import="java.util.Enumeration" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/1/10
  Time: 15:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    /**
     *服务号分享链接中转通道
     *通过该通道访问的链接，可以得到服务号对访问用户（无论是否关注服务号）的授权功能，并得到code
     *https://open.weixin.qq.com/connect/oauth2/authorize?appid=服务号的APPID
     *&redirect_uri=中转地址?
     *shareurl=获取code后要访问的地址
     *&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect
     **/

    //例：链接
    //https://open.weixin.qq.com/connect/oauth2/authorize?appid=服务号ID&redirect_uri=http%3A%2F%2Fwww.xxxxx.com%2Fa%2FskipShare.jsp%3Fshareurl%3Dhttp%3A%2F%2Fwww.abc.com%2Findex%2Findex.do&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect
    String url = request.getParameter("shareurl");
    String code = request.getParameter("code");
    int index = url.indexOf("?");
    if(index>0){
        url = url.substring(0,index);
    }

    //动态获取参数，并重新拼接
    Enumeration rnames=request.getParameterNames();
    StringBuffer sb = new StringBuffer();
    sb.append(url);
    int open = 0;
    for (Enumeration e = rnames ; e.hasMoreElements() ;) {
        String thisName=e.nextElement().toString();
        String thisValue=request.getParameter(thisName);
        if(open==0){
            sb.append("?").append("from").append("=").append("hsh");
            open = 1;
        }else{
            sb.append("&").append(thisName).append("=").append(thisValue);
        }

    }
    url = sb.toString();

    response.sendRedirect(url);

%>
